package logic

import (
	"context"
	"time"
	"xialou_platform/app/errorx"
	"xialou_platform/services/agent/model"

	"xialou_platform/services/agent/rpc/agentClient"
	"xialou_platform/services/agent/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CourseAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCourseAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CourseAddLogic {
	return &CourseAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CourseAddLogic) CourseAdd(in *agentClient.Course) (*agentClient.IdBody, error) {
	id := l.svcCtx.Flake.Generate().Int64()
	nowTime := time.Now()
	_, err := l.svcCtx.CourseModel.Insert(l.ctx, &model.Course{
		Id:         id,
		Cover:      in.Cover,
		Title:      in.Title,
		Type:       in.Type,
		Tag:        in.Tag,
		Video:      in.Video,
		Content:    in.Content,
		CreateTime: nowTime,
		UpdateTime: nowTime,
		Online:     true,
		DelFlag:    false,
		Sort:       in.Sort,
	})
	if err != nil {
		return nil, errorx.NewReasonError("网络出小差了，请重试刷新再试。", err.Error())
	}
	return &agentClient.IdBody{
		Id: id,
	}, nil
}
